package com.leochan.shuniu.database.service;

import java.util.List;

import com.leochan.shuniu.database.BaseService;
import com.leochan.shuniu.database.DatabaseManager;
import com.leochan.shuniu.database.dao.UserDao;
import com.leochan.shuniu.database.model.User;

public class UserService extends BaseService {

	private static UserDao dao = new UserDao(DatabaseManager.getInstance()
			.getDatabase());

	public static User getUserById(long id) {
		return dao.queryById(id);
	}

	public static List<User> getUserByUsername(String userName, boolean isFuzzy) {
		List<User> users = dao.queryBy(String.format("%s %s ?",
				UserDao.Username, isFuzzy ? "=" : "like"),
				new String[] { userName });
		return users;
	}

	public static List<User> getAllUser() {
		return dao.queryAll();
	}

	public static void addOrUpdateUser(User user) {
		dao.saveOrUpdate(user);
	}

	public static void deleteUserById(long id) {
		dao.deleteById(id);
	}

	public static void deleteUser(User user) {
		dao.delete(user);
	}

	public static void deleteUserByToken(String token) {
		dao.deleteBy(String.format("%s = ?", UserDao.Token,
				new String[] { token }));
	}

}
